int print(char *to_print, int flush) {
	static char buffer[PRINT_BUFFER]  = "";
	static unsigned int length = 0;
	static unsigned int lines  = 0;
	static unsigned int x      = 0;
	
	static char where = {1, 23};
	
	static const int coord_length = 2;
	
	unsigned int
		breaks  = 0,
		current = x,
		i       = 0,
		total   = 0;
	
	/* Count how many lines and characters there are in the passed string.
	 */
	while (to_print[i] != '\0') {
		total++;
		
		if (to_print[i] == '\t') {
			current += TAB - current % TAB;
			
			if (current >= TERMINAL_WIDTH) {
				breaks++;
				current = 0;
			}
		} else if (to_print[i] == '\r' || to_print[i] == '\n') {
			breaks++;
			current = 0;
		} else if (current == TERMINAL_WIDTH) {
			breaks++;
			current = 1;
		} else {
			current++;
		}
		
		i++;
	}
	
	/* If adding the current string would make the buffer too long (in lines or
	 * characters), flush the buffer and start over.
	 */
	if (
		(lines + breaks >= MAX_LINES || length + total >= PRINT_BUFFER - 1) &&
		length > 0
	) {
		sys_req(WRITE, TERMINAL, buffer, &length);
		length = lines = x = 0;
		
		sys("pause");
		where[1] = wherey() - 2;
		sys_req(GOTO, TERMINAL, where, &coord_length);
	}
	
	/* While there are too many characters or lines to fit in the buffer, pass
	 * everything straight to sys_req so only that which will fit is buffered.
	 */
	while (lines + breaks > MAX_LINES || length + total >= PRINT_BUFFER - 1) {
		int local_breaks = 0;
		i = current = 0;
		
		while (i < PRINT_BUFFER - 1 && local_breaks < MAX_LINES) {
			if (to_print[i] == '\t') {
				current += TAB - current % TAB;
				
				if (current >= TERMINAL_WIDTH) {
					local_breaks++;
					breaks--;
					current = 0;
				}
			} else if (to_print[i] == '\r' || to_print[i] == '\n') {
				local_breaks++;
				breaks--;
				current = 0;
			} else if (current == TERMINAL_WIDTH) {
				local_breaks++;
				breaks--;
				current = 1;
			} else {
				current++;
			}
			
			i++;
		}
		
		sys_req(WRITE, TERMINAL, to_print, &i);
		sys("pause");
		where[1] = wherey() - 2;
		sys_req(GOTO, TERMINAL, where, &coord_length);
		
		to_print += i;
		total -= i;
	}
	
	/* Copy all remaining characters to the buffer, incrementing the appropriate
	 * counts.
	 */
	 while (to_print[i] != '\0') {
		if (to_print[i] == '\t') {
			x += TAB - x % TAB;
			
			if (x >= TERMINAL_WIDTH) {
				lines++;
				x = 0;
			}
		} else if (to_print[i] == '\r' || to_print[i] == '\n') {
			lines++;
			x = 0;
		} else if (x == TERMINAL_WIDTH) {
			lines++;
			x = 1;
		} else {
			x++;
		}
		buffer[length++] = to_print[i];
		i++;
	}
	 
	buffer[length] = '\0';
	 /* 
	  * If flush is equal true, print everything from buffer.
	  */
	if (
		(lines + breaks >= MAX_LINES || length + total >= PRINT_BUFFER - 1) &&
		length > 0
	) {
		sys_req(WRITE, TERMINAL, buffer, &length);
		length = lines = x = 0;
	}
}